package cn.kailangtu.myapplication.data;

import android.util.Log;

import cn.kailangtu.serialport.CommonUtils;

public class CanID {

    private int 地址;
    private Float 偏移量;
    private Integer 比特位;
    private Float 精度;
    private int 数据索引下标;
    private int 数据长度;
    private Integer 高位位置;
    private Integer 最小值;

    public static CanID build(String canID)
    {
        if(canID.indexOf("CAN") > -1) {

            CanID result = new CanID();
            if (canID.indexOf("CAN") > 0) {
                canID = canID.substring(canID.indexOf("CAN"));
            }
            if(canID.indexOf("H")> -1) {
                String strAddress = canID.substring(3, canID.indexOf("H"));
                if ((strAddress.length() % 2) == 1) {
                    strAddress = "0" + strAddress;
                }
                result.set地址( CommonUtils.hexToInt(strAddress));
            }

            Integer bitIndex = getInnerIntValue(canID,"S");
            Float jingdu = getInnerFloatValue(canID,"J");
            Float pianyi = getInnerFloatValue(canID,"P");
            String strByte = getInnerString(canID,"H");
            Integer highPosition = getInnerIntValue(canID,"L");
            Integer minValue = getInnerIntValue(canID,"Z");
            if(bitIndex != null)
            {
                Log.d("CAN ID INFO:","BIT INDEX:"+bitIndex);
            }
            if(jingdu != null)
            {
                Log.d("CAN ID INFO:","JING DU:"+jingdu);
            }
            if(pianyi != null){
                Log.d("CAN ID INFO:","PIAN YI:"+pianyi);
            }
            if(highPosition != null)
            {
                Log.d("CAN ID INFO:","HIGH POSITION:"+highPosition);
            }
            if(minValue != null)
            {
                Log.d("CAN ID INFO:","MIN VALUE:"+minValue);
            }
            int dataIndex = 0;
            if(strByte.length()>0)
            {
                //如果没有指明用哪个BYTE，那么默认拿第一个
                dataIndex = Integer.parseInt(strByte.substring(0,1));
            }
            result.set偏移量(pianyi);
            result.set数据索引下标(dataIndex);
            result.set比特位(bitIndex);
            result.set数据长度(strByte.length());
            result.set精度(jingdu);
            result.set高位位置(highPosition);
            result.set最小值(minValue);

            return result;
        }

        return null;
    }

    public int get地址() {
        return 地址;
    }

    public void set地址(int 地址) {
        this.地址 = 地址;
    }

    public Float get偏移量() {
        return 偏移量;
    }

    public void set偏移量(Float 偏移量) {
        this.偏移量 = 偏移量;
    }

    public Integer get比特位() {
        return 比特位;
    }

    public void set比特位(Integer 比特位) {
        this.比特位 = 比特位;
    }

    public Float get精度() {
        return 精度;
    }

    public void set精度(Float 精度) {
        this.精度 = 精度;
    }

    public int get数据索引下标() {
        return 数据索引下标;
    }

    public void set数据索引下标(int 数据索引下标) {
        this.数据索引下标 = 数据索引下标;
    }

    public int get数据长度() {
        return 数据长度;
    }

    public void set数据长度(int 数据长度) {
        this.数据长度 = 数据长度;
    }

    public Integer get高位位置() {
        return 高位位置;
    }

    public void set高位位置(Integer 高位位置) {
        this.高位位置 = 高位位置;
    }

    public Integer get最小值() {
        return 最小值;
    }

    public void set最小值(Integer 最小值) {
        this.最小值 = 最小值;
    }

    static String[] element = new String[]{"H","S","J","P","L","Z"};

    private static String getInnerString(String source,String startChar)
    {
        String result = "";

        if(source != null && source.indexOf(startChar)>-1)
        {
            source = source.substring(source.indexOf(startChar)+1);
            for(String c : element){
                if(source.indexOf(c)> -1)
                {
                    source = source.substring(0,source.indexOf(c));
                }
            }
            result = source;
        }

        return result;
    }

    private static Integer getInnerIntValue(String source,String startChar)
    {
        Log.d("GETALLCHILD", "SOURCE:" + source+" ||| STARTCHAR:"+startChar);
        Integer result = null;
        String strResult = getInnerString(source,startChar);
        if(strResult.length()>0)
        {
            if(strResult.startsWith("_"))
            {
                strResult = strResult.substring(1);
                int intResult = Integer.valueOf(strResult);
                result = 0-intResult;
            }
            else{
                result = Integer.valueOf(strResult);;
            }
        }
        return result;
    }

    private static Float getInnerFloatValue(String source,String startChar){
        Float result = null;
        String strResult = getInnerString(source,startChar);
        if(strResult.length()>0)
        {
            if(strResult.startsWith("_"))
            {
                strResult = strResult.substring(1);
                float intResult = Float.valueOf(strResult);
                result = 0-intResult;
            }
            else{
                result = Float.valueOf(strResult);;
            }
        }
        return result;
    }
}
